<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="apply_cate_id" :data="cateList" :props="cateProps" :current-node-key="''"
							 :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" @click="addCate()" v-auth="'applyCate.add'"></el-button>
					<el-button type="success" size="small" icon="el-icon-edit" @click="editCate()" v-auth="'applyCate.edit'"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" @click="delCate" v-auth="'applyCate.delete'"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'apply.add'"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'apply.delete'"></el-button>
					<sc-file-import :apiObj="$API.apply.apply.import" @success="success" ref="importBox" :templateUrl="`${config.API_URL}/apply.XLS`"
									v-if="showImpoet"></sc-file-import>
					<sc-export-button :auth="'apply.export'" :apiObj="apiObj" :search="search"></sc-export-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.apply_code" placeholder="供应商编号" clearable @keyup.enter="upsearch"></el-input>
						<el-input v-model="search.apply_name" placeholder="供应商名称" clearable @keyup.enter="upsearch"></el-input>
						<el-input v-model="search.linkman" placeholder="联系人" clearable @keyup.enter="upsearch"></el-input>
						<el-input v-model="search.mobile" placeholder="联系电话" clearable @keyup.enter="upsearch"></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="primary" @click="unsetSearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50" align="center"></el-table-column>
					<el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
					<el-table-column label="分类名称" prop="cate.apply_cate_name" align="center"></el-table-column>
					<el-table-column label="供应商编号" prop="apply_code" align="center"></el-table-column>
					<el-table-column label="供应商名称" prop="apply_name" align="center"></el-table-column>
					<el-table-column label="联系人" prop="linkman" align="center"></el-table-column>
					<el-table-column label="联系电话" prop="mobile" align="center"></el-table-column>
					<el-table-column label="联系地址" prop="address" align="center"></el-table-column>
					<!-- <el-table-column label="状态" prop="system_disable" align="center">
						<template #default="scope">
							<el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
						</template>
					</el-table-column> -->
					<el-table-column label="简介" prop="desc" align="center"></el-table-column>
					<el-table-column label="添加时间" prop="create_time" align="center"></el-table-column>
					<el-table-column label="状态" prop="system_disable" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.system_disable==0" type="success">启用</el-tag>
							<el-tag v-if="scope.row.system_disable==1" type="danger">禁用</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'apply.show'">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'apply.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'apply.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>

				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
	<cate-save-dialog v-if="dialog.cateSave" ref="cateSaveDialog" @success="handleSuccessCate" @closed="dialog.cateSave = false"></cate-save-dialog>
</template>

<script>
import saveDialog from './save'
import cateSaveDialog from './cateSave.vue'
import config from "@/config"
export default {
	name: '供应商列表',
	components: {
		saveDialog,
		cateSaveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
				cateSave: false
			},
			showGrouploading: false,
			groupFilterText: '',
			cateList: [],
            cateProps: {
				label: (data) => {
					return data.apply_cate_name
				}
			},
			apiObj: this.$API.apply.apply.lists,
			selection: [],
			search: {
				apply_code: null,
				apply_name: null,
				linkman: null,
				mobile: null,
			},
			config:config,
			showImpoet: this.$TOOL.data.get("PERMISSIONS").includes('apply.import')
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		addCate() {
			this.dialog.cateSave = true
			this.$nextTick(() => {
				this.$refs.cateSaveDialog.open('add')
			})
		},
		editCate() {
			var CheckedNodes = this.$refs.group.getCurrentNode()
			if (!CheckedNodes.apply_cate_id) {
				this.$message.warning("请选择需要修改的项")
				return false;
			}
			this.dialog.cateSave = true
			this.$nextTick(() => {
				this.$refs.cateSaveDialog.open('edit').setData(CheckedNodes)
			})
		},
		async delCate() {
			var CheckedNodes = this.$refs.group.getCurrentNode()
			if (!CheckedNodes.apply_cate_id) {
				this.$message.warning("请选择需要删除的项")
				return false;
			}

			var confirm = await this.$confirm('确认删除已选择的分类吗？', '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).catch(() => { })
			if (confirm != 'confirm') {
				return false
			}

			this.menuloading = true
			var reqData = {
				apply_cate_id: CheckedNodes.apply_cate_id
			}
			var res = await this.$API.apply.applyCate.delete.post(reqData)
			this.menuloading = false

			if (res.code == 1) {
				this.getGroup()
			} else {
				this.$message.warning(res.message)
			}
		},
		//添加
		add() {
			// var CheckedNodes = this.$refs.group.getCurrentNode()
			// let cate_id = '';
			// if (CheckedNodes.id) {
			// 	cate_id = CheckedNodes.id
			// }
            // .setData({ cate_id: cate_id })
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = { apply_id: row.apply_id }
			var res = await this.$API.apply.apply.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, '提醒', {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { apply_id: delete_ids }
			var res = await this.$API.apply.apply.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, '提醒', {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.apply_id)
			})
			this.$confirm(`确定批量删除选中的 ${this.selection.length}项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.apply.apply.update_status.post({
				apply_id: row.apply_id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success(this.$t('message.yes'));
			} else {
				this.$alert(res.message, '提醒', {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.apply.applyCate.all.get();
			this.showGrouploading = false;
			var allNode = { apply_cate_id: '', apply_cate_name: '所有' }
			res.data.unshift(allNode);
			this.cateList = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.apply_cate_name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			var params = {
				apply_cate: data.apply_cate_id
			}
			this.$refs.table.reload(params)
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		unsetSearch() {
			Object.keys(this.search).forEach(key => {
				this.search[key] = null;
			});
			this.upsearch()
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
		handleSuccessCate() {
			this.getGroup()
		}
	}
}
</script>

<style></style>
